﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Terminal;
using Senparc.Weixin.Helpers.Extensions;
using Senparc.Weixin.Work.AdvancedAPIs;
using Senparc.Weixin.Work.AdvancedAPIs.MailList;
using Senparc.Weixin.Work.Containers;
using Senparc.Weixin.Work.Helpers;
using WeiXinAPI.Common;

namespace WeiXinAPI
{
    public class WxCorpInfoHandler : BaseHandler
    {
        #region 获取企业关系列表 GetWxCorpInfoList

        [MustLogin]
        [Description("获取企业关系列表")]
        [ReturnType(typeof(IList<CorpWX_Info>))]
        public ApiResult GetWxCorpInfoList()
        {
            var dc = new TerminalEntities();
            var list = dc.CorpWX_Info.Where(x => x.Enable).ToList();
            return Success(list);
        }

        #endregion

        #region 添加 AddWxCorpInfo

        [JsonType]
        [MustLogin]
        [Description("添加")]
        [ReturnType(typeof(int))]
        public ApiResult AddWxCorpInfo(CorpWX_Info info)
        {
            var dc = new TerminalEntities();
            info.CreateTime = DateTime.Now;
            info.Enable = true;
            info.CIdentification = Guid.NewGuid();
            dc.CorpWX_Info.Add(info);
            var result = dc.SaveChanges();
            return Success(result);
        }

        #endregion

        #region 修改 UpdateWxCorpInfo

        [JsonType]
        [MustLogin]
        [Description("修改")]
        [ReturnType(typeof(int))]
        public ApiResult UpdateWxCorpInfo(CorpWX_Info info)
        {
            if (info.CID <= 0)
            {
                return Error("修改失败！找不到对应的企业信息");
            }

            var dc = new TerminalEntities();
            var model = dc.CorpWX_Info.FirstOrDefault(x => x.CID == info.CID && x.Enable);
            if (model == null)
            {
                return Error("修改失败！找不到对应的企业信息");
            }

            //model.CIdentification = info.CIdentification;
            model.CorpID = info.CorpID;
            model.CorpSercet = info.CorpSercet;
            model.CommunicationSercet = info.CommunicationSercet;
            model.AgentId = info.AgentId;
            model.AgentName = info.AgentName;
            model.CorpName = info.CorpName;

            var result = dc.SaveChanges();
            return Success(result);
        }

        #endregion

        #region 获取 GetWxCorpInfo

        [MustLogin]
        [Description("获取")]
        [ReturnType(typeof(int))]
        public ApiResult GetWxCorpInfo(int CID)
        {
            if (CID <= 0)
            {
                return Error("参数异常，请重新再试");
            }

            var dc = new TerminalEntities();
            var model = dc.CorpWX_Info.FirstOrDefault(x => x.CID == CID && x.Enable);
            if (model != null)
            {
                return Success(model);
            }
            return Error("找不到对应的模板信息，请重新再试");
        }

        #endregion

        #region 删除 RemoveWxCorpInfo

        [MustLogin]
        [Description("删除")]
        [ReturnType(typeof(int))]
        public ApiResult RemoveWxCorpInfo(int CID)
        {
            if (CID <= 0)
            {
                return Error("参数异常，请重新再试");
            }

            var dc = new TerminalEntities();
            var model = dc.CorpWX_Info.FirstOrDefault(x => x.CID == CID && x.Enable);
            if (model != null)
            {
                model.Enable = false;
                var result = dc.SaveChanges();
                return Success(result);
            }
            return Error("删除失败！找不到对应的企业信息");
        }

        #endregion

    }
}
